package com.atguigu.tingshu.payment.strategy;

import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.common.util.AuthContextHolder;
import com.atguigu.tingshu.model.user.UserInfo;
import com.atguigu.tingshu.payment.config.WxPayV3Config;
import com.atguigu.tingshu.user.client.UserInfoFeignClient;
import com.atguigu.tingshu.vo.payment.WeChatPayVo;
import com.wechat.pay.java.core.RSAAutoCertificateConfig;
import com.wechat.pay.java.service.payments.jsapi.JsapiServiceExtension;
import com.wechat.pay.java.service.payments.jsapi.model.Amount;
import com.wechat.pay.java.service.payments.jsapi.model.Payer;
import com.wechat.pay.java.service.payments.jsapi.model.PrepayRequest;
import com.wechat.pay.java.service.payments.jsapi.model.PrepayWithRequestPaymentResponse;

public interface PaymentStrategy {

    // 支付请求抽象方法
    public abstract WeChatPayVo createJsapi(String paymentType, String orderNo);

    // 获取支付类型
    public abstract String getPaymentType() ;

    default public WeChatPayVo requestWeChatPay(RSAAutoCertificateConfig rsaAutoCertificateConfig , WxPayV3Config wxPayV3Config ,
                                                String description , String orderNo , UserInfoFeignClient userInfoFeignClient) {

        // 调用微信支付下单接口
        /**
         * 请求微信支付平台的jsapi的下单接口
         */
        JsapiServiceExtension service = new JsapiServiceExtension.Builder().config(rsaAutoCertificateConfig).build();

        // 创建PrepayRequest对象，通过该对象封装请求参数
        PrepayRequest prepayRequest = new PrepayRequest() ;
        prepayRequest.setAppid(wxPayV3Config.getAppid());
        prepayRequest.setMchid(wxPayV3Config.getMerchantId());
        prepayRequest.setDescription(description);                       // 描述信息， 可以把订单的标题作为描述信息
        prepayRequest.setOutTradeNo(orderNo);
        prepayRequest.setNotifyUrl(wxPayV3Config.getNotifyUrl());

        // 设置支付的金额
        // BigDecimal orderAmount = orderInfo.getOrderAmount();
        Amount amount = new Amount();
        amount.setTotal(1);          // 支付金额的单位是分 , 使用假的支付金额，方便后期进行测试
        prepayRequest.setAmount(amount);

        // 获取当前登录的用户的id
        Long userId = AuthContextHolder.getUserId();
        Result<UserInfo> userInfoResult = userInfoFeignClient.findByUserId(userId);
        UserInfo userInfo = userInfoResult.getData();
        Payer payer = new Payer();
        payer.setOpenid(userInfo.getWxOpenId());                  // 当前登录用户所对应的微信的openId
        prepayRequest.setPayer(payer);

        // 发起请求
        PrepayWithRequestPaymentResponse prepayWithRequestPaymentResponse = service.prepayWithRequestPayment(prepayRequest);

        // 从PrepayWithRequestPaymentResponse对象中获取结果数据，然后将结果数据封装到WeChatPayVo对象
        WeChatPayVo weChatPayVo = new WeChatPayVo() ;
        weChatPayVo.setTimeStamp(prepayWithRequestPaymentResponse.getTimeStamp());
        weChatPayVo.setNonceStr(prepayWithRequestPaymentResponse.getNonceStr());
        weChatPayVo.setPackageVal(prepayWithRequestPaymentResponse.getPackageVal());
        weChatPayVo.setSignType(prepayWithRequestPaymentResponse.getSignType());
        weChatPayVo.setPaySign(prepayWithRequestPaymentResponse.getPaySign());

        return weChatPayVo ;
    }
}
